#!/usr/bin/node
const EventEmitter = require('events').EventEmitter;// eslint-disable-line no-unused-vars

function Dog(name,energy){
  let _listeners = {
    
  };
  let _name = name,//私有属性
      _energy = energy;//私有属性


  let timer = setInterval(()=>{
    if (_energy>0){
      emit('bark');//事件：bark事件
      _energy--;
    }else{
      clearInterval(timer);
    }
  },1000);
  this.name = ()=> _name;//方法
  this.energy = ()=>_energy;//方法
  this.on =  (evt,fn)=>{
    //validate _listeners[evt] exist
    if(typeof(_listeners[evt])==='undefined'){
      _listeners[evt] = [];
    }
    
    _listeners[evt].push(fn);
  };

  function emit(evt,arg){
    if(typeof(_listeners[evt])==='undefined'){
      return;
    }
    _listeners[evt].forEach((fn)=>{
      fn.call(this,arg);
    });
  }
}


module.exports = Dog;
